package com.hakgc.rent.web.servlet;

import com.hakgc.rent.constant.SystemConstant;
import com.hakgc.rent.entity.District;
import com.hakgc.rent.entity.Street;
import com.hakgc.rent.service.DistrictService;
import com.hakgc.rent.service.StreetService;
import com.hakgc.rent.service.impl.DistrictServiceImpl;
import com.hakgc.rent.service.impl.StreetServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 访问区县列表页面的Servlet
 *
 * @author Xing.Jiejian
 * @date 2020/11/9
 */
@WebServlet(name = "StreetListServlet", urlPatterns = "/rent/streets")
public class StreetListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 查询分页的街道数据
        // 分页参数有页码，每页显示数量
        int pageNo = 1;
        String strPageNo = request.getParameter("pageNo");
        if (strPageNo != null && !"".equals(strPageNo)) {
            pageNo = Integer.parseInt(strPageNo);
        }
        int pageSize = SystemConstant.PAGE_SIZE;
        String strPageSize = request.getParameter("pageSize");
        if (strPageSize != null && !"".equals(strPageSize)) {
            pageSize = Integer.parseInt(strPageSize);
        }
        // 获取查询条件
        String name = request.getParameter("streetName");
        if (name != null) {
            name = name.trim();
        }
        int did = -1;
        String strDid = request.getParameter("did");
        if (strDid != null && !"".equals(strDid)) {
            did = Integer.parseInt(strDid);
        }

        // 查询区县集合（在页面组装下拉菜单搜索条件）
        DistrictService districtService = new DistrictServiceImpl();
        List<District> districts = districtService.findAll(false);

        // 查询分页列表
        StreetService streetService = new StreetServiceImpl();

        // 获取街道的总条数
        int totalCount = streetService.getCount(name,did);
        // 计算总页码数
        int pageCount = (totalCount + pageSize - 1) / pageSize;
        // 限定页码范围不能超限
        if (pageNo < 1) {
            // 如果页码小于第一页，则直接访问第一页
            pageNo = 1;
        }
        if (pageNo > pageCount) {
            // 如果页码超过最大页，则显示最后一页
            pageNo = pageCount;
        }

        // 查询获取分页区县集合(不需要关联街道信息)
        List<Street> streets = streetService.findByPage(name,did,pageNo,pageSize,true);


        // 跳转到区县页，并且需要将区县集合在页面上渲染
        // 将区县集合放入request对象中，
        request.setAttribute("districts", districts);
        request.setAttribute("streets", streets);
        request.setAttribute("pageNo", pageNo);
        request.setAttribute("totalCount", totalCount);
        request.setAttribute("pageCount", pageCount);
        request.setAttribute("streetName", name);
        request.setAttribute("did", did);
        // 转发到对应的页面上
        request.getRequestDispatcher("/rent/streetsList.jsp").forward(request, response);
    }
}
